package com.heihei.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class DateFormatUtil {
    public static Date parse(String str, String pattern, Locale locale) {
        if(str == null || pattern == null) {
            return null;
        }
        try {
            if(locale == null) {
                return new SimpleDateFormat(pattern).parse(str);
            }
            return new SimpleDateFormat(pattern, locale).parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date parse(String str, String pattern) {
        return parse(str, pattern, null);
    }


    public static String format(Date date, String pattern, Locale locale) {
        if(date == null || pattern == null) {
            return null;
        }
        if(locale == null) {
            return new SimpleDateFormat(pattern).format(date);
        }
        return new SimpleDateFormat(pattern, locale).format(date);
    }

    public static String format(Date date, String pattern) {
        return format(date, pattern, null);
    }



    public static void main(String[] args) {
        String dateStr = "Sun Dec 31 19:00:00 CST 1899";
        Date date = DateFormatUtil.parse(dateStr, "EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
        System.out.println(date);
    }
}
